/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanControlInputMathExpressions.java
 *
 * Created on 26-jul-2011, 15:57:31
 */

package br.usp.ime.faguilar.guis.capturers;

import br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanWriting;
import MathExpression.Graphics.Drawable;
import MathExpression.Graphics.GMathExpression;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 *
 * @author frank
 */
public class PanControlInputMathExpressions extends javax.swing.JPanel{

//     JScrollPane pictureScrollPane;
    /** Creates new form PanControlInputMathExpressions */
    public PanControlInputMathExpressions() {
        initComponents();
        EditWritingHandler toggleButtonshandler=new EditWritingHandler();
        this.toggleButtonEraseSymbol.addItemListener(toggleButtonshandler);
        this.toggleButtonLabel.addItemListener(toggleButtonshandler);
        this.toggleButtonWrite.addItemListener(toggleButtonshandler);
        this.panWriting.setPanControl(this);
//        pictureScrollPane = new JScrollPane(panWriting1);
//        pictureScrollPane.setPreferredSize(panWriting1.getSize());
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        butGroupEditWriting = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        toggleButtonWrite = new javax.swing.JToggleButton();
        toggleButtonEraseSymbol = new javax.swing.JToggleButton();
        toggleButtonLabel = new javax.swing.JToggleButton();
        butClear = new javax.swing.JButton();
        butUndo = new javax.swing.JButton();
        butRedo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        barTimeAdvance = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        panWriting = new br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanWriting();

        setLayout(new java.awt.BorderLayout());

        butGroupEditWriting.add(toggleButtonWrite);
        toggleButtonWrite.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/1311711651_drawing_pen.png"))); // NOI18N
        toggleButtonWrite.setSelected(true);
        toggleButtonWrite.setToolTipText("Write expressions");
        toggleButtonWrite.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        toggleButtonWrite.setIconTextGap(2);
        toggleButtonWrite.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        butGroupEditWriting.add(toggleButtonEraseSymbol);
        toggleButtonEraseSymbol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete.png"))); // NOI18N
        toggleButtonEraseSymbol.setToolTipText("Delete symbol");
        toggleButtonEraseSymbol.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        toggleButtonEraseSymbol.setIconTextGap(8);
        toggleButtonEraseSymbol.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        butGroupEditWriting.add(toggleButtonLabel);
        toggleButtonLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/35px-Pi-symbol.png"))); // NOI18N
        toggleButtonLabel.setToolTipText("Label symbol");
        toggleButtonLabel.setAutoscrolls(true);
        toggleButtonLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        toggleButtonLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        butClear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Actions-edit-clear-icon.png"))); // NOI18N
        butClear.setToolTipText("Clear");
        butClear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        butClear.setIconTextGap(2);
        butClear.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        butClear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butClearMouseClicked(evt);
            }
        });

        butUndo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/_active__undo.png"))); // NOI18N
        butUndo.setToolTipText("Undo");
        butUndo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        butUndo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        butUndo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butUndoMouseClicked(evt);
            }
        });

        butRedo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/_active__redo.png"))); // NOI18N
        butRedo.setToolTipText("Redo");
        butRedo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        butRedo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        butRedo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butRedoMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(28, 28, 28)
                .add(toggleButtonWrite, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(toggleButtonEraseSymbol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(toggleButtonLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 172, Short.MAX_VALUE)
                .add(butUndo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(butRedo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(butClear, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 62, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(butClear, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(butRedo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(butUndo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(toggleButtonWrite, 0, 0, Short.MAX_VALUE)
                        .add(toggleButtonLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(toggleButtonEraseSymbol, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.SOUTH);

        jScrollPane1.setBorder(null);

        panWriting.setPreferredSize(new java.awt.Dimension(500, 200));

        org.jdesktop.layout.GroupLayout panWritingLayout = new org.jdesktop.layout.GroupLayout(panWriting);
        panWriting.setLayout(panWritingLayout);
        panWritingLayout.setHorizontalGroup(
            panWritingLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 645, Short.MAX_VALUE)
        );
        panWritingLayout.setVerticalGroup(
            panWritingLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 270, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(panWriting);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, barTimeAdvance, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(barTimeAdvance, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void butClearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butClearMouseClicked
        // TODO add your handling code here:
        clearPanel();
        
}//GEN-LAST:event_butClearMouseClicked

    public void updateScroll(Rectangle r){
        this.jScrollPane1.setViewportView(panWriting);
        panWriting.scrollRectToVisible(r);
    }

    private void butUndoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butUndoMouseClicked
        // TODO add your handling code here:
        this.panWriting.beforeExpressMatch();
    }//GEN-LAST:event_butUndoMouseClicked

    private void butRedoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butRedoMouseClicked
        // TODO add your handling code here:
        panWriting.nextExpressMatch();
    }//GEN-LAST:event_butRedoMouseClicked

    public void clearPanel(){
        this.panWriting.clearPanel();
    }

    public void setVisibleLabelingButton(boolean visible){
        this.toggleButtonLabel.setVisible(visible);
    }
    private class EditWritingHandler implements ItemListener{
        public void itemStateChanged(ItemEvent e) {
            if(e.getSource()==toggleButtonEraseSymbol){
                if(toggleButtonEraseSymbol.isSelected()){
                    panWriting.setState(PanWriting.STATE_DELETING_SYMBOL);
                }
            }else if(e.getSource()==toggleButtonLabel){
                if(toggleButtonLabel.isSelected()){
                    panWriting.setState(PanWriting.STATE_LABELING);
                }
            }else if(e.getSource()==toggleButtonWrite){
                if(toggleButtonWrite.isSelected()){
                    panWriting.setState(PanWriting.STATE_WRITING);
                }
            }
        }
    }

    public GMathExpression getGMathExpression(){
        return this.panWriting.getgMathExpressionWritten();
    }

    public PanWriting getPanWriting() {
        return panWriting;
    }

    public void setAdvanceBarValue(int advance,int total){
        this.barTimeAdvance.setMaximum(total);
        this.barTimeAdvance.setValue(advance);
    }

    public void setDrawable(Drawable drawable) {
        panWriting.setDrawable(drawable);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar barTimeAdvance;
    private javax.swing.JButton butClear;
    private javax.swing.ButtonGroup butGroupEditWriting;
    private javax.swing.JButton butRedo;
    private javax.swing.JButton butUndo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanWriting panWriting;
    private javax.swing.JToggleButton toggleButtonEraseSymbol;
    private javax.swing.JToggleButton toggleButtonLabel;
    private javax.swing.JToggleButton toggleButtonWrite;
    // End of variables declaration//GEN-END:variables

}
